<?php

/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/9/24
 * Time: 8:59
 */
namespace app\backend\middleware;

use app\common\controller\ReturnData;
use think\Db;

class TransactionMiddleware
{

    use ReturnData;

    public function handle($request, \Closure $next)
    {
        Db::startTrans();
        try {
            $responseData = $next($request);
            Db::commit();
            if(!empty($responseData->getData()))return $this->ok($responseData->getData());
            return $this->ok();
        } catch (\Exception $e) {
            Db::rollback();
            throw $e;
        }
    }

}